package com.bykj.taketurns.activity.adapter;

import android.annotation.TargetApi;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bykj.taketurns.R;
import com.bykj.taketurns.fragment.HomeTextFragment;

import java.util.List;


/**
 * Created by Administrator on 2016/12/28.
 */

public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener{
    private List<Fragment>list;
    private RadioGroup radioGroup;
    private FragmentActivity fragmentActivity;
    private int fragmentContentId;//存放fragment的容器
    private int currentTab;
    int []imgs=new int[]{
            R.drawable.hopeful,R.drawable.tryed, R.drawable.play
    };
    int [] imgsPress=new int[]{
            R.drawable.hopeful_pressed, R.drawable.try_pressed, R.drawable.play_pressed
    };
    public FragmentTabAdapter(List<Fragment>list, RadioGroup radioGroup, FragmentActivity fragmentActivity, int fragmentContentId){
        this.list=list;
        this.radioGroup=radioGroup;
        this.fragmentActivity=fragmentActivity;
        this.fragmentContentId=fragmentContentId;
        FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, list.get(0),"0");
        ft.commitAllowingStateLoss();
        showTab(0);
        radioGroup.setOnCheckedChangeListener(this);


    }


    @Override
    public void onCheckedChanged(RadioGroup raGroup, int i) {
        for(int j=0;j<raGroup.getChildCount();j++){
            if(raGroup.getChildAt(j).getId()==i){
               Fragment fragment = list.get(j);
               FragmentTransaction transaction = obtainFragmentTransaction(j);
               getCurrentFragment().onPause();
               if(fragment.isAdded()){
                   fragment.onResume();
               }else {
                   transaction.add(fragmentContentId,fragment,j+"");
               }
               showTab(j);
                transaction.commitAllowingStateLoss();
           }

        }
    }
    /**
     * 获取一个带切换动画的fragment
     */
    public FragmentTransaction obtainFragmentTransaction(int index){
        FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
//        if(index>currentTab){
//            transaction.setCustomAnimations(R.anim.right_enter,R.anim.right_exit);
//        }else {
//            transaction.setCustomAnimations(R.anim.left_enter,R.anim.left_exit);
//        }
        //将当前的导航页面索引赋值
        currentTab=index;
        return  transaction;
    }
    /**
     * 获取当前的fragment
     *
     */
    public Fragment getCurrentFragment(){
        return  list.get(currentTab);
    }
    /**
     * 展示当前的页面
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void showTab(int index){
        if(index==0){
            RadioButton childAt = (RadioButton) radioGroup.getChildAt(index);
            Drawable drawable = fragmentActivity.getDrawable(imgsPress[index]);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            childAt.setCompoundDrawables(null, null, null, drawable);
        }

        for(int i=0;i<list.size();i++){
            FragmentTransaction transaction = obtainFragmentTransaction(i);
            RadioButton childAt = (RadioButton) radioGroup.getChildAt(i);
            if(i==index){
                transaction.show(list.get(i));
                Drawable drawable = fragmentActivity.getDrawable(imgsPress[i]);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                childAt.setCompoundDrawables(null, null, null, drawable);
            }else {
                transaction.hide(list.get(i));
                Drawable drawable = fragmentActivity.getDrawable(imgs[i]);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                childAt.setCompoundDrawables(null, null, null, drawable);
            }


            //提交
            transaction.commitAllowingStateLoss();
            hideTextFragment(index);
        }
        //将当前的导航页面索引赋值
        currentTab=index;
    }
    /**
     * 获取当前的页面
     */
    public int getCurrentTab() {
        return currentTab;
    }
    /**
     *强制隐藏当前首页的文字页面
     */
    public void hideTextFragment(int index){
        if(index!=0){
            FragmentManager manager = fragmentActivity.getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            HomeTextFragment fragment = (HomeTextFragment) manager.findFragmentByTag("text");
            if(fragment!=null){
                transaction.hide(fragment);

            }
            transaction.commit();

        }
    }
}
